﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Threading;
using log4net;
using System.IO;
using NSsh.Common.Utility;
using System.Diagnostics;
using System.Reflection;

namespace NSsh.Server.Service
{
    static class Program
    {
        private static ILog log = LogManager.GetLogger(typeof(Program));

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Thread.CurrentThread.Name = "ServerMain";

            // Change the working directory from System32 to our install directory
            Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            // configure logging
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("logging.xml"));
            Trace.Listeners.Add(new Log4netListener());

            // Setup exception handler
            AppDomain.CurrentDomain.UnhandledException += UnhandledException;

#if DEBUG_SEVICE
            Service service = new Service();
            service.Start();

            while (true)
            {
                Thread.Sleep(1000);
            }
#else
            ServiceBase.Run(new Service());
#endif
        }

        static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            log.Fatal("Unhandled exception in NSsh.", e.ExceptionObject as Exception);
        }
    }
}
